import 'package:flutter/material.dart';

import 'package:provide/provide.dart';
import '../provide/details_info.dart';

//import 'package:flutter_screenutil/flutter_screenutil.dart';
//页面widget
import './details_page_widget/details_top.dart';
import './details_page_widget/details_explain.dart';
import './details_page_widget/details_web.dart';
import './details_page_widget/details_mytabs.dart';
import './details_page_widget/details_bottom.dart';
//flutter 数据持久化 sqflite shared_preferences file
class DetailsPage extends StatelessWidget {
  final String goodsId;
  const DetailsPage({Key key,this.goodsId}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Provide.value<DetailsInfoProvide>(context).changeInintstate();
    //this._getBackInfo(context);
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: Icon(Icons.arrow_back),
          onPressed: (){
            Navigator.pop(context);
          }
        ),
        title: Text('商品详情页'),
        centerTitle: true,
      ),
      body:FutureBuilder(
         future: _getBackInfo(context),
         builder:_buildFuture
      )
    );
  }
  Widget _buildFuture(BuildContext context, AsyncSnapshot snapshot) {
    switch (snapshot.connectionState) {
      case ConnectionState.none:
        print('还没有开始网络请求');
        return Text('还没有开始网络请求');
      case ConnectionState.active:
        print('active');
         return Center(
          child: CircularProgressIndicator(),
        );
      case ConnectionState.waiting:
        print('waiting');
        return Center(
          child: CircularProgressIndicator(),
        );
      case ConnectionState.done:
        print('done');
        if (snapshot.hasError) return Text('Error: ${snapshot.error}');
        return Provide<DetailsInfoProvide>(
          builder: (context,child,detailsInfoProvide){
            if( detailsInfoProvide.goodsinfo !=null && !detailsInfoProvide.isloading){
              return Column(
                children: <Widget>[
                  Expanded(
                    child:ListView(
                      children: <Widget>[
                        DetailsTopArea(),
                        DetailsExplain(),
                        DetailsTabBar(),
                        DetailsWeb()
                      ],
                    ),
                  ),
                  DetailsBottom()
                ],
              );
            }else{
              return Center(
                child: CircularProgressIndicator(),
              );
            }
          },
        );
      default:
        return Center(
          child: CircularProgressIndicator(),
        );
    }
  }

  Future _getBackInfo(BuildContext context) async{
    await Provide.value<DetailsInfoProvide>(context).getGoodsInfo(goodsId);
    return '完成加载';
  }
}
